package com.cooper.mediadb.local;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import com.cooper.mediadb.MediaMainActivity;
import com.cooper.scrapemedialib.R;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class VideoSelectDialog extends DialogFragment implements
		OnItemClickListener {

	private ListView listView;

	public static VideoSelectDialog newInstance(File videoFile,
			boolean showPlayerContinueDialog) {
		VideoSelectDialog result = new VideoSelectDialog();
		Bundle bundle = new Bundle();
		bundle.putSerializable("VIDEO_FILE", videoFile);
		bundle.putBoolean("SHOW_PLAYER_CONTINUEDIALOG",
				showPlayerContinueDialog);
		result.setArguments(bundle);
		return result;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_TITLE, R.style.SrcSelectDialogTheme);
		setCancelable(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View result = inflater.inflate(R.layout.video_select_dialog, container,
				false);
		listView = (ListView) result.findViewById(android.R.id.list);
		listView.setOnItemClickListener(this);
		listView.setAdapter(new VideoItemAdapter(inflater,
				(File) getArguments().getSerializable("VIDEO_FILE")));
		return result;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		dismiss();
		if (getActivity() instanceof MediaMainActivity) {
			File videoFile = (File) parent.getItemAtPosition(position);
			List<Uri> videoUris = new ArrayList<Uri>(1);
			videoUris.add(Uri.fromFile(videoFile));
			((MediaMainActivity) getActivity()).playMedia(videoUris, LocalMedia
					.fileNameToTitle(videoFile),
					getArguments().getBoolean("SHOW_PLAYER_CONTINUEDIALOG"));
		}
	}

	private class VideoItemAdapter extends BaseAdapter implements
			OnClickListener {

		private LayoutInflater inflater;
		private List<File> videoFiles;

		public VideoItemAdapter(LayoutInflater inflater, File videoFile) {
			super();
			this.inflater = inflater;
			if (videoFile.isFile()) {
				this.videoFiles = new LinkedList<File>();
				this.videoFiles.add(videoFile);
			} else {
				videoFiles = new LinkedList<File>(Arrays.asList(videoFile
						.listFiles(new LocalMedia.VideoFileFilter())));
			}
		}

		@Override
		public int getCount() {
			return videoFiles.size();
		}

		@Override
		public Object getItem(int position) {
			return videoFiles.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View result = null;
			ViewHolder holder = null;
			if (convertView != null) {
				result = convertView;
				holder = (ViewHolder) convertView.getTag();
			} else {
				result = inflater.inflate(R.layout.video_select_item, null);
				holder = new ViewHolder();
				holder.titleView = (TextView) result
						.findViewById(R.id.videoTitle);
				holder.deleteButton = (ImageButton) result
						.findViewById(R.id.deleteVideoButton);
				holder.deleteButton.setOnClickListener(this);
				result.setTag(holder);
			}
			holder.titleView.setText(LocalMedia.fileNameToTitle(videoFiles
					.get(position)));
			holder.deleteButton.setTag(Integer.valueOf(position));
			return result;
		}

		private final class ViewHolder {
			TextView titleView;
			ImageButton deleteButton;
		}

		@Override
		public void onClick(View v) {
			int position = (Integer) v.getTag();
			videoFiles.get(position).delete();
			videoFiles.remove(position);
			if (videoFiles.size() < 2) {
				dismiss();
			} else
				this.notifyDataSetChanged();

		}

	}

}
